﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Store
{
    public partial class 原料发货 : System.Web.UI.Page
    {
        protected string Notice = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string list = Request.QueryString["ID"];
                if (list != null && list!="")
                {
                    this.IDList.Text = list;
                    string id1 = list.Split(",".ToCharArray())[0];
                    this.SqlDataSource1.SelectCommand =
@"declare @c1 int,@c2 int,@c3 int,@c4 int;
select @c1=COUNT(distinct 原料ID),@c2=COUNT(distinct 供货ID),@c3=count(distinct 分厂),@c4=count(distinct 库区) from 原料到发记录 where ID in (" + list + @") and 状态=2;
if @c1!=1 or @c2!=1 or @c3!=1 or @c4!=1 begin set @原料ID=0;return;end;
select @原料ID=t1.原料ID,@供货=t2.名称,@供货ID=t1.供货ID,@分厂=分厂,@库区=库区 from 原料到发记录 t1
left outer join 原料供货单位 t2 on t2.id=t1.供货ID where t1.id =" + id1 + 
@";select @磅重=SUM(磅重),@支数=SUM(总支数) from 原料到发记录 where id in(" + list + ");";
                    //Response.Write(this.SqlDataSource1.SelectCommand);
                    this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                }
                else
                {
                    this.Button_Send.Visible = false;
                }

                this.Date1.Text = DateTime.Today.ToLongDateString();
            }
        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            int leibie=(int)e.Command.Parameters[0].Value;
            if (leibie == 0)
            {
                this.Notice = "请选择相同原料类别、供货商、分厂、库区的合格未发货记录发货。";
                this.Button_Send.Visible = false;
            }
            else
            {
                this.LeiBie.Text = leibie.ToString();
                if (!Convert.IsDBNull(e.Command.Parameters[2].Value))
                {
                    this.Label_GongHuo.Text = (string)e.Command.Parameters[2].Value;
                }
                
                int fc=(int)e.Command.Parameters[1].Value;
                if (fc == 0)
                {
                    this.FenChang.Enabled = true;
                }
                else
                {
                    this.FenChang.Text = fc.ToString();
                }
                this.Label_BangZhong.Text = ((double)e.Command.Parameters[3].Value).ToString();
                this.Label_ZhiShu.Text = ((int)e.Command.Parameters[4].Value).ToString();
                this.Label_KuQu.Text = ((int)e.Command.Parameters[5].Value).ToString();
                this.Label_GongHuoID.Text = ((int)e.Command.Parameters[6].Value).ToString();
            }

        }

        protected void Button_Send_Click(object sender, EventArgs e)
        {
            DateTime d;
            decimal bz=0;
            decimal.TryParse(this.Label_BangZhong.Text, out bz);
            string op = (string)this.Session["OperatorName"];
            if (op == null)
            {
                op = "";
            }

            if (!DateTime.TryParse(this.Date1.Text, out d))
            {
                this.Notice = "“发货日期”录入有误";
                return;
            }
            else if (this.FenChang.Text == "0")
            {
                this.Notice = "请选择“使用单位”";
                return;
            }

            if (this.FenChang.Enabled)
            {
                this.SqlDataSource1.UpdateCommand = "update 原料到发记录 set 分厂=" + this.FenChang.Text + " where ID in(" + this.IDList.Text + ") and 状态=2;";
            }
            else
            {
                this.SqlDataSource1.UpdateCommand = "";
            }
            this.SqlDataSource1.UpdateCommand += "update 原料到发记录 set 状态=3,发货日期='"
                + d.ToShortDateString() + "' where ID in(" + this.IDList.Text + ") and 状态=2;insert into 原料发货记录"
                +"(原料ID,供货ID,分厂,库区,日期,重量,支数,库管员,明细)values("+this.LeiBie.Text+","+this.Label_GongHuoID.Text
                +","+this.FenChang.Text+","+this.Label_KuQu.Text+",'"+d.ToShortDateString()+"',"+((int)(bz*1000))+","
                +this.Label_ZhiShu.Text+",'"+op+"','"+this.IDList.Text+"')";
            //Response.Write(this.SqlDataSource1.UpdateCommand);
            //Response.End();
            if (this.SqlDataSource1.Update() > 0)
            {
                this.Notice = "发货操作成功";
                this.Button_Send.Visible = false;
            }
            else
            {
                this.Notice = "发货操作失败";
            }
        }

    }
}
